<?php
namespace Shopmobile\Controller;
use Think\Controller;
class GiftController extends ExtendController {
	public function index(){
		$newgifts = S($this->sid.$this->uid."newgifts");
		if($newgifts){
			$gift_count = count($newgifts);
			$this->assign("newgifts",$newgifts);
		}
		$UGFT = D("UserGifts");
		$UGFT->sid=$this->sid;
		$UGFT->uid=$this->uid;
		$UGFT->limit="0,10";
		$gifts = $UGFT->loadMyGifts();
		$all_count = $UGFT->getAllCount();

		$this->assign("type_count",count($gifts));
		$this->assign("all_count",$all_count);
		$this->assign("gifts",$gifts);
		$this->assign("gift_count",$gift_count);
		$this->assign("ts",time());
		$this->display();
	}
	public function gift(){
		$gid = I("gid");
		$gtp = I("gtp");
		$gtpn = I("gtpn");
		$pic = I("pic");
		$name = I("name");
		
		$UGFT = D("UserGiftsView");
		$UGFT->sid = $this->sid;
		$UGFT->uid = $this->uid;
		$UGFT->gtp = $gtp;
		$UGFT->gift_id = $gid;
		$UGFT->limit = "0,10";
		$gifts = $UGFT->showMyGifts();

		if($gtp==4){
			// 读取卡密列表
			$KM = D("MainKm");
			$KM->gid = $gid;
			$KM->sid = $this->sid;
			$KM->shopuid = $this->uid;
			$km = $KM->getKmInGift();
		}
		
		$this->assign("km",$km);
		$this->assign("gid",$gid);
		$this->assign("gtp",$gtp);
		$this->assign("gtpn",$gtpn);
		$this->assign("pic",$pic);
		$this->assign("name",$name);
		$this->assign("gifts",$gifts);
		$this->display();
	}
	public function get_gift_log(){
		$gid = I("gid");
		$gtp = I("gtp");
		$index = I("index");
		
		$UGFT = D("UserGiftsView");
		$UGFT->sid = $this->sid;
		$UGFT->uid = $this->uid;
		$UGFT->gtp = $gtp;
		$UGFT->gift_id = $gid;
		$UGFT->limit = "$index+1,10";
		$gifts["gifts"] = array_values($UGFT->showMyGifts());
		$gifts["all_count"] = $UGFT->getAllCount();
		
		$this->ajaxReturn($gifts);
	}
	public function pickup(){
		$giftid=I("gid");
		$mygid=I("mygid");
		$getway=I("getway");
		$oid=I("order");
		$password=I("passwd");
		// $eid=I("eid");
		// $tgiftid=I("tgiftid"); //增加抽奖机会的奖品id
		// $name=I("name"); //增加抽奖机会的奖品id
		$show=I("show"); //分享的回调参数，用于判断在哪个渠道分享。
		$way=I("way"); //分享的是否是天猫或淘宝
		$show_title=I("show_title"); //分享的是否是天猫或淘宝
		$show_content=I("show_content"); //分享的是否是天猫或淘宝

		$GIFT = D("Gifts");
		
		$GIFT->getway = $getway=="null"?null:$getway;
		$GIFT->giftid = $giftid;
		$GIFT->oid = $oid;
		$GIFT->password = $password;
		// $GIFT->eid = $eid;
		// $GIFT->name = $name;
		$GIFT->sid = $this->sid;
		$GIFT->shop_uid = $this->shop_uid;
		$GIFT->mixednick = $this->mixednick;
		$GIFT->show = $show;
		$GIFT->way = $way;
		$GIFT->mygid = $mygid;
		$GIFT->show_title = $show_title;
		$GIFT->show_content = $show_content;
		$data = $GIFT->pickup();
		
		$this->ajaxReturn($data);
	}
	public function get_gift_jifen(){
		$gid = I("gid");
		$getway = I("getway");
		
		$GIFT = D("EventsGifts");
		$gw["id"] = $gid;
		$gw["sid"] = $this->sid;
		$gw["ext_rule"] = $getway;
		$jifen = $GIFT->where($gw)->getField("jifen");
		
		$this->ajaxReturn(Array("jifen"=>$jifen));
	}
	public function get_gift_metal(){
		$gid = I("gid");
		$getway = I("getway");
		
		$GIFT = D("EventsGifts");
		$gw["id"] = $gid;
		$gw["sid"] = $this->sid;
		$gw["ext_rule"] = $getway;
		$medal = current($GIFT->where($gw)->getField("medal_id,medal_quant,medal_name"));
		
		$this->ajaxReturn($medal);
	}
	public function get_gift_addcart(){
		$gid = I("gid");
		$getway = I("getway");
		
		$GIFT = D("EventsGifts");
		$gw["id"] = $gid;
		$gw["sid"] = $this->sid;
		$gw["ext_rule"] = $getway;
		$data = $GIFT->where($gw)->getField("cart_items");
		if(C("TAE")){
			$data = json_decode(urldecode_utf8($data));
		}else{
			$data = json_decode(urldecode($data));
		}
		
		$COLL = M("UserGiftsItemaddcart");
		$cowh["sid"]=$this->sid;
		$cowh["egid"]=$gid;
		$cowh["user_id"]=$this->shop_uid;
		$items = $COLL->where($cowh)->getField("id,item_iid");
		if(!is_array($items)){
			$items=Array($items);
		}
		foreach($data as $k=>$item){
			foreach($items as $cli){
				if($item[0]==$cli){
					unset($data[$k]);
				}
			}
		}
		
		$data = $data?array_values($data):Array();
		$this->ajaxReturn(Array("cart_items"=>$data));	
	}
	public function get_gift_collect(){
		$gid = I("gid");
		$getway = I("getway");
		
		$GIFT = D("EventsGifts");
		$gw["id"] = $gid;
		$gw["sid"] = $this->sid;
		$gw["ext_rule"] = $getway;
		$data = $GIFT->where($gw)->getField("collect_items");
		if(C("TAE")){
			$data = json_decode(urldecode_utf8($data));
		}else{
			$data = json_decode(urldecode($data));
		}
		
		$COLL = M("UserGiftsItemcollect");
		$cowh["sid"]=$this->sid;
		$cowh["egid"]=$gid;
		$cowh["user_id"]=$this->shop_uid;
		$items = $COLL->where($cowh)->getField("id,item_iid");
		if(!is_array($items)){
			$items=Array($items);
		}
		foreach($data as $k=>$item){
			foreach($items as $cli){
				if($item[0]==$cli){
					unset($data[$k]);
				}
			}
		}
		$data = $data?array_values($data):Array();
		$this->ajaxReturn(Array("collect_items"=>$data));
	}
	public function check_gift_friendshow(){
		$egid = I("gid");
		$mygid = I("mygid");
		$GFS = M("UserGiftsFriendshow");
		$gwh["sid"]=$this->sid;
		$gwh["user_id"]=$this->shop_uid;
		$gwh["egid"]=$egid;
		$gwh["mygid"]=$mygid;
		$re = $GFS->where($gwh)->getField("id");
		$reda["hasError"]=!$re;
		$this->ajaxReturn($reda);
	}
	public function get_gift_friendshow(){
		$gid = I("gid");
		$mygid = I("mygid");
		$getway = I("getway");
		$show_qcode = I("show_qcode");
		$GIFT = D("EventsGifts");
		$gw["id"] = $gid;
		$gw["sid"] = $this->sid;
		$gw["ext_rule"] = $getway;
		$data = current($GIFT->where($gw)->getField("id,pic_url,show_title,show_content"));
		if(!$data){
			$reda["hasError"]=true;
			$reda["msg"]="抱歉，没有这个奖品！";
		}else{
			$data["show_url"] = C("URL")."?c=shoptaemob&sid=".$this->sid;
			$reda["show"] = $data;
		}
		
		if($show_qcode && $data){
			if(C("TAE"))
			{
				$qrcode = $cacheService->get("gqr".implode($gw));
			}
			else
			{
				$qrcode = S("gqr".implode($gw));
			}
			if(!$qrcode){
				// 码上淘API免费无需节约资源
				Vendor('taobaosdk.top.request.MaQrcodeCommonCreateRequest');
				$req = new \MaQrcodeCommonCreateRequest;
				$req->setType("url");
				$req->setContent(C("URL")."?m=shoptaemob&a=taepc_friendshow&sid=".$this->sid."&shop_uid=".$this->shop_uid."&gid=".$gid."&mygid=".$mygid."&getway=".$getway."&pic_url=".$data["pic_url"]); //长度不能大于256，隐藏以下：."&show_title=".$data["show_title"]."&show_content=".$data["show_content"] 
				$req->setName("多麦抽奖");
				$req->setChannelName("移动端装修");
				$req->setStyle("EF4F2B");
				$req->setSize("300");
				$req->setNeedEps("false");
				$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");
				
				$c = new \TopClient;
				$c->appkey = C("SM_KEY");
				$c->secretKey = C("SM_SECRET");
				
				$sessionKey = M("ShopCfg")->where(Array("sid"=>$this->sid))->getField("sessionkey");
				$resp = $c->execute($req, $sessionKey);

				if(C("TAE")){
					$qrcode = (String)$resp->modules->qrcode_d_o[0]->qrcode_url;
				}else{
					$qrcode = (String)$resp->modules->qrcode_d_o->qrcode_url;
				}
			}
			if($resp->code){
				$reda["qrerr"]=(Array)$resp;
			}
			$reda["qrcode"]=$qrcode;
		}
		
		$this->ajaxReturn($reda);
	}
	public function get_new_gifts(){
		// $newgifts = S($this->sid.$this->uid."newgifts"); //读取缓存中的奖品。除非重设猜分数据（即清空缓存奖品），否则不再判断是否获得奖品
		if(!$newgifts)
		{
			$GIFT = D("Gifts");
			$GIFT->sid = $this->sid;
			$GIFT->uid = $this->uid;
			$newgifts = $GIFT->getWinGiftsByGuessDiff(); //使用数据库中已记录的猜分数据判断奖品
			S($this->sid.$this->uid."newgifts",$newgifts);
		}
		$re["gifts"] = $newgifts;
		
		$UGFT = D("UserGifts");
		$UGFT->sid=$this->sid;
		$UGFT->uid=$this->uid;
		$re["all_count"] = $UGFT->getAllCount();
		$this->ajaxReturn($re);
	}
	public function get_my_gifts(){
		$index = I("index");
		$UGFT = D("UserGifts");
		$UGFT->sid=$this->sid;
		$UGFT->uid=$this->uid;
		$UGFT->limit="$index+1,10";
		$gifts["gifts"] = array_values($UGFT->loadMyGifts());
		$gifts["all_count"] = $UGFT->getGroupCount();
		$this->ajaxReturn($gifts);
	}
	public function add_share_log(){
		
		$this->ajaxReturn();
	}
}